package cn.cnsy123.blog.application.controller;

import cn.cnsy123.blog.application.http.base.BaseResponse;
import cn.cnsy123.blog.application.http.response.ArticleDetailResponse;
import cn.cnsy123.blog.application.http.response.LastArticleResponse;
import cn.cnsy123.blog.application.service.ArticleQueryService;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * @author yangpengfei
 * @descrption 查询一篇博文
 * @date 2019-10-17 14:38:00
 */
@RestController
public class ArticleQueryController {

    private static final Logger logger = LoggerFactory.getLogger(ArticleQueryController.class);

    @Autowired
    private ArticleQueryService articleQueryService;

    /**
     * 查询博文数据
     * @param articleId
     * @return
     */
    @GetMapping(value = "/article/detail/{articleId}")
    public BaseResponse<ArticleDetailResponse> queryArticleDetail(@PathVariable(value = "articleId",required = false) Long articleId){
        logger.info("博客客户端-用户查询博文-文章ID:{}",articleId);
        BaseResponse<ArticleDetailResponse> baseResponse = new BaseResponse<ArticleDetailResponse>();
        baseResponse.setCode("0000");
        baseResponse.setMessage("系统请求成功");
        baseResponse.setRequestTime(new Date(System.currentTimeMillis()));
        ArticleDetailResponse articleDetailResponse = null;
        if(articleId == null){
            logger.error("博客客户端-用户查询博文-文章ID为空，将返回无数据状态，前端解析成404模式");
            articleDetailResponse = new ArticleDetailResponse("1009","博文请求参数为空");
        }else{
            try {
                articleDetailResponse = articleQueryService.queryArticle(articleId);
            } catch (Exception e) {
                logger.error("博客客户端-用户查询博文-查询异常",e);
                articleDetailResponse = new ArticleDetailResponse("1099","博文查询异常");
            }
        }
        baseResponse.setData(articleDetailResponse);
        baseResponse.setResponseTime(new Date(System.currentTimeMillis()));
        logger.info("博客客户端-用户查询博文-返回报文:{}", JSON.toJSONString(baseResponse));
        return baseResponse;
    }

    @GetMapping(value = "/article/top5")
    public BaseResponse<LastArticleResponse> queryLastArticleDetail(){
        logger.info("博客客户端-博文页面-获取最新文章");
        BaseResponse<LastArticleResponse> baseResponse = new BaseResponse<LastArticleResponse>();
        baseResponse.setCode("0000");
        baseResponse.setMessage("系统请求成功");
        baseResponse.setRequestTime(new Date(System.currentTimeMillis()));
        LastArticleResponse lastArticleResponse = null;
        try {
            lastArticleResponse = articleQueryService.queryLastArticleDetail();
        } catch (Exception e) {
            logger.error("博客客户端-用户查询博文-查询异常",e);
            lastArticleResponse = new LastArticleResponse("1099","博文查询异常");
        }
        baseResponse.setData(lastArticleResponse);
        baseResponse.setResponseTime(new Date(System.currentTimeMillis()));
        logger.info("博客客户端-博文页面-用户查询最新博文-返回报文:{}", JSON.toJSONString(baseResponse));
        return baseResponse;
    }
}
